package com.huawei.hms.common.internal;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import com.huawei.hms.adapter.AvailableAdapter;
import com.huawei.hms.adapter.AvailableAdapter.AvailableCallBack;
import com.huawei.hms.adapter.BinderAdapter;
import com.huawei.hms.adapter.BinderAdapter.BinderCallBack;
import com.huawei.hms.api.ConnectionResult;
import com.huawei.hms.api.HuaweiApiAvailability;
import com.huawei.hms.api.HuaweiApiClientImpl;
import com.huawei.hms.api.IPCTransport;
import com.huawei.hms.core.aidl.d;
import com.huawei.hms.support.api.ResolveResult;
import com.huawei.hms.support.api.client.AidlApiClient;
import com.huawei.hms.support.api.client.ResultCallback;
import com.huawei.hms.support.api.client.SubAppInfo;
import com.huawei.hms.support.api.core.ConnectService;
import com.huawei.hms.support.api.entity.core.DisconnectInfo;
import com.huawei.hms.support.api.entity.core.DisconnectResp;
import com.huawei.hms.support.log.HMSLog;
import com.huawei.hms.utils.Util;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

public abstract class BaseHmsClient implements AidlApiClient {
    private static final Object a = new Object();
    private final Context b;
    private String c;
    private final ClientSettings d;
    private volatile d e;
    private final AtomicInteger f = new AtomicInteger(1);
    private final ConnectionCallbacks g;
    private final OnConnectionFailedListener h;
    private Handler i = null;
    private BinderAdapter j;
    protected String sessionId;

    public interface ConnectionCallbacks {
        public static final int CAUSE_API_CLIENT_EXPIRED = 3;
        public static final int CAUSE_NETWORK_LOST = 2;
        public static final int CAUSE_SERVICE_DISCONNECTED = 1;

        void onConnected();

        void onConnectionSuspended(int i);
    }

    public interface OnConnectionFailedListener {
        void onConnectionFailed(ConnectionResult connectionResult);
    }

    class a implements ResultCallback<ResolveResult<DisconnectResp>> {
        final /* synthetic */ BaseHmsClient a;

        private a(BaseHmsClient baseHmsClient) {
            this.a = baseHmsClient;
        }

        public /* synthetic */ void onResult(Object obj) {
            a((ResolveResult) obj);
        }

        public void a(final ResolveResult<DisconnectResp> resolveResult) {
            new Handler(Looper.getMainLooper()).post(new Runnable(this) {
                final /* synthetic */ a b;

                public void run() {
                    this.b.a.a(resolveResult);
                }
            });
        }
    }

    public BaseHmsClient(Context context, ClientSettings clientSettings, OnConnectionFailedListener onConnectionFailedListener, ConnectionCallbacks connectionCallbacks) {
        this.b = context;
        this.d = clientSettings;
        this.c = this.d.getAppID();
        this.h = onConnectionFailedListener;
        this.g = connectionCallbacks;
    }

    public void onConnecting() {
        connectionConnected();
    }

    protected final void connectionConnected() {
        a(3);
        if (this.g != null) {
            this.g.onConnected();
        }
    }

    public String getServiceAction() {
        return HuaweiApiAvailability.SERVICES_ACTION;
    }

    private void a() {
        HMSLog.i("BaseHmsClient", "enter bindCoreService");
        this.j = new BinderAdapter(this.b, getServiceAction(), com.huawei.hms.utils.a.a(this.b).b());
        this.j.binder(new BinderCallBack(this) {
            final /* synthetic */ BaseHmsClient a;

            {
                this.a = r1;
            }

            public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
                HMSLog.i("BaseHmsClient", "Enter onServiceConnected.");
                this.a.e = com.huawei.hms.core.aidl.d.a.a(iBinder);
                if (this.a.e == null) {
                    this.a.j.unBind();
                    this.a.a(1);
                    this.a.b(10);
                    return;
                }
                this.a.onConnecting();
            }

            public void onBinderFailed(int i) {
                this.a.b(10);
                this.a.e = null;
            }

            public void onServiceDisconnected(ComponentName componentName) {
                HMSLog.i("BaseHmsClient", "Enter onServiceDisconnected.");
                this.a.a(1);
                if (this.a.g != null) {
                    this.a.g.onConnectionSuspended(1);
                }
            }
        });
    }

    public String getAppID() {
        return this.c;
    }

    public boolean isConnected() {
        return this.f.get() == 3 || this.f.get() == 4;
    }

    public boolean isConnecting() {
        return this.f.get() == 5;
    }

    public void disconnect() {
        int i = this.f.get();
        HMSLog.i("BaseHmsClient", "Enter disconnect, Connection Status: " + i);
        switch (i) {
            case 3:
                a(4);
                c();
                return;
            case 5:
                b();
                a(4);
                return;
            default:
                return;
        }
    }

    private void a(int i) {
        this.f.set(i);
    }

    private void b() {
        synchronized (a) {
            if (this.i != null) {
                this.i.removeMessages(2);
                this.i = null;
            }
        }
    }

    private void c() {
        ConnectService.disconnect(new HuaweiApiClientImpl(this.b), new DisconnectInfo(this.d.getScopes(), this.d.getApiName())).setResultCallback(new a());
    }

    private void a(ResolveResult<DisconnectResp> resolveResult) {
        HMSLog.i("BaseHmsClient", "Enter onDisconnectionResult, disconnect from server result: " + resolveResult.getStatus().getStatusCode());
        this.j.unBind();
        a(1);
    }

    public void checkAvailabilityAndConnect() {
        HMSLog.i("BaseHmsClient", "====== HMSSDK version: 30003300 ======");
        int i = this.f.get();
        HMSLog.i("BaseHmsClient", "Enter connect, Connection Status: " + i);
        if (i != 3 && i != 5 && i != 4) {
            a(5);
            i = getMinApkVersion();
            HMSLog.i("BaseHmsClient", "connect minVersion:" + i);
            AvailableAdapter availableAdapter = new AvailableAdapter(i);
            i = availableAdapter.isHuaweiMobileServicesAvailable(this.b);
            HMSLog.i("BaseHmsClient", "check available result: " + i);
            if (i == 0) {
                a();
            } else if (availableAdapter.isUserResolvableError(i)) {
                a(availableAdapter);
            } else {
                b(i);
            }
        }
    }

    private void a(AvailableAdapter availableAdapter) {
        if (getClientSettings().isHasActivity()) {
            Activity activeActivity = Util.getActiveActivity(getClientSettings().getCpActivity(), getContext());
            if (activeActivity != null) {
                availableAdapter.startResolution(activeActivity, new AvailableCallBack(this) {
                    final /* synthetic */ BaseHmsClient a;

                    {
                        this.a = r1;
                    }

                    public void onComplete(int i) {
                        if (i == 0) {
                            this.a.a();
                        } else {
                            this.a.b(i);
                        }
                    }
                });
                return;
            } else {
                b(26);
                return;
            }
        }
        b(26);
    }

    private void b(int i) {
        HMSLog.i("BaseHmsClient", "notifyFailed result: " + i);
        if (this.h != null) {
            this.h.onConnectionFailed(new ConnectionResult(i));
        }
    }

    public void connect() {
        checkAvailabilityAndConnect();
    }

    public d getService() {
        return this.e;
    }

    protected final void checkConnected() {
        if (!isConnected()) {
            throw new IllegalStateException("Not connected. Call connect() and wait for onConnected() to be called.");
        }
    }

    public String getSessionId() {
        return this.sessionId;
    }

    public int getMinApkVersion() {
        return 30000000;
    }

    public Context getContext() {
        return this.b;
    }

    public SubAppInfo getSubAppInfo() {
        return this.d.getSubAppID();
    }

    public String getPackageName() {
        return this.d.getClientPackageName();
    }

    public String getCpID() {
        return this.d.getCpID();
    }

    public String getTransportName() {
        return IPCTransport.class.getName();
    }

    public List<String> getApiNameList() {
        return this.d.getApiName();
    }

    protected ClientSettings getClientSettings() {
        return this.d;
    }
}
